/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.logiwareinc.studentdetspring.hibernate.dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Rajesh
 */
public class HibernateUtil {

    private static SessionFactory sessionFactory;
    private static Session session = null;

    public Session getSession() throws Exception {
        if (sessionFactory == null) {
            sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        if (session == null || !session.isOpen()) {
            session = sessionFactory.openSession();
        }
        return session;
    }

    public List executeQuery(String queryString) throws Exception {
        Query query = getSession().createQuery(queryString);
        return query.list();
    }
}
